package com.elric.mall.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.elric.mall.dto.ZgProvincesNode;
import com.elric.mall.dto.ZgZzjgNode;
import com.elric.mall.enums.ImportType;
import com.elric.mall.mapper.ZgBaseZzjgMapper;
import com.elric.mall.model.ZgBaseZzjg;
import com.elric.mall.model.ZgBaseZzjgExample;
import com.elric.mall.model.ZgSProvinces;
import com.elric.mall.model.ZgSProvincesExample;
import com.elric.mall.service.ZgBaseZzjgService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 实现功能：战规-组织机构实现层
 *
 * @author 作者[Yuxuan.he]
 * @version 1.0.00  2021-09-16 11:21
 */
@Service
public class ZgBaseZzjgServiceImpl implements ZgBaseZzjgService {
    @Autowired
    private ZgBaseZzjgMapper zgBaseZzjgMapper;

    @Override
    public int create(ZgBaseZzjg zgBaseZzjg) {
        zgBaseZzjg.setId(IdUtil.fastUUID());
        return zgBaseZzjgMapper.insert(zgBaseZzjg);
    }

    @Override
    public int update(ZgBaseZzjg zgBaseZzjg) {
        return zgBaseZzjgMapper.updateByPrimaryKeySelective(zgBaseZzjg);
    }

    @Override
    public ZgBaseZzjg findById(String id) {
        return zgBaseZzjgMapper.selectByPrimaryKey(id);
    }

    @Override
    public int delete(String id) {
        return zgBaseZzjgMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<ZgBaseZzjg> list(Integer pageSize, Integer pageNum, ZgBaseZzjg zgBaseZzjg) {
        PageHelper.startPage(pageNum, pageSize);
        ZgBaseZzjgExample example = new ZgBaseZzjgExample();
        if(!StringUtils.isEmpty(zgBaseZzjg.getText())) {
            example.createCriteria().andTextLike("%"+zgBaseZzjg.getText()+ "%");
        }
        return zgBaseZzjgMapper.selectByExample(example);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertBatch(List<ZgBaseZzjg> list, ImportType importType) {
        if (importType == ImportType.full) {
            ZgBaseZzjgExample example = new ZgBaseZzjgExample();
            zgBaseZzjgMapper.deleteByExample(example);
        }
        list.stream().forEach(l -> {
            l.setId(IdUtil.fastUUID());
        });
        return zgBaseZzjgMapper.insertBatch(list);
    }

    /**
     * 将zzjg实体转化为树形结构
     */
    private ZgZzjgNode covertProvincesNode(ZgBaseZzjg base, List<ZgBaseZzjg> menuList) {
        ZgZzjgNode node = new ZgZzjgNode();
        BeanUtils.copyProperties(base, node);
        node.setLabel(node.getText());
        List<ZgZzjgNode> children = menuList.stream()
                .filter(subMenu -> subMenu.getParentInfoId().equals(base.getId()))
                .map(subMenu -> covertProvincesNode(subMenu, menuList)).collect(Collectors.toList());
        node.setChildren(children);
        return node;
    }

    @Override
    public List<ZgZzjgNode> findeZzjgTree() {
        ZgBaseZzjgExample example = new ZgBaseZzjgExample();
        example.setOrderByClause("order_number desc");
        List<ZgBaseZzjg> list = zgBaseZzjgMapper.selectByExample(example);
        List<ZgZzjgNode> result = list.stream()
                .filter(r -> StrUtil.isBlank(r.getParentInfoId()))
                .map(r -> covertProvincesNode(r, list)).collect(Collectors.toList());
        return result;
    }
}
